package com.evl.wxuser.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.evl.common.constant.factory.PageFactory;
import com.evl.common.controller.BaseController;
import com.evl.common.persistence.dao.WxchatUserMapper;
import com.evl.common.persistence.model.WxchatUser;
import com.evl.constant.TrainTicketStatus;
import com.evl.constant.UserGender;
import com.evl.core.util.Convert;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;
import com.evl.utils.ObjectUtil;
import com.evl.utils.filter.Filter;

/**
 * 微信用户列表控制器
 *
 * @author fengshuonan
 * @Date 2017-06-14 16:36:15
 */
@Controller
@RequestMapping("/api/wxMembers")
@Api(value = "小程序用户")
public class WxMembersController extends BaseController {

    private Logger log = Logger.getLogger(this.getClass());
	
    private String PREFIX = "/wxMembers/";

    @Autowired
    private WxchatUserMapper WxchatUserMapper;
    
    /**
     * 跳转到微信用户列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "wxMembers.html";
    }

    /**
     * 跳转到添加微信用户列表
     */
    @RequestMapping("/wxMembers_add")
    public String wxMembersAdd() {
        return PREFIX + "wxMembers_add.html";
    }

    /**
     * 跳转到修改微信用户列表
     */
    @RequestMapping("/wxMembers_update/{wxMembersId}")
    public String wxMembersUpdate(@PathVariable Integer wxMembersId, Model model) {
        return PREFIX + "wxMembers_edit.html";
    }

    /**
     * 获取微信用户列表列表
     */
	@RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String nickName) {
    	Page<WxchatUser> page = new PageFactory<WxchatUser>().defaultPage();
        List<WxchatUser> result =  WxchatUserMapper.getWxchatUsers(page, nickName);
        page.setRecords(ObjectUtil.toListMap(result, new Filter() {
			@Override
			public Object filtration(String fieldName, Object o) {
				if(fieldName.equals("gender")){
					o= UserGender.valueOf(Integer.parseInt(String.valueOf(o)));
				}
				return o;
			}
		}));
        return super.packForBT(page);
    }

    /**
     * 新增微信用户列表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 删除微信用户列表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改微信用户列表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return super.SUCCESS_TIP;
    }

    /**
     * 微信用户列表详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
    
    /**
     * 小程序用户添加或修改
     * @param wxchatUser
     */
    @ResponseBody
    @ApiOperation(value="小程序用户添加或修改",httpMethod = "POST", response = WxchatUser.class, notes = "小程序用户添加或修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public BaseResultVo addOrUpdate(@ApiParam(required = true, name = "wxchatUser", value = "小程序用户添加或修改") @RequestBody WxchatUser wxchatUser){
    	WxchatUser wxchatUserVO = new WxchatUser();
    	
    	if(!StringUtils.isEmpty(wxchatUser.getOpendId())){
    		wxchatUserVO = WxchatUserMapper.getWxchatUserByOpendId(wxchatUser.getOpendId());
    		
    		if(null == wxchatUserVO){
    			WxchatUserMapper.addWxchatUser(wxchatUser);
    		}else{
    			WxchatUserMapper.updateWxchatUserByOpendId(wxchatUser);
    		}
    	}
    	return JsonReturnHelper.getSuccessWithData(wxchatUserVO);
    	
    }
}
